#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int> > permute(vector<int> &num) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    vector<vector<int> > res;
    res.push_back(num);
    if(num.size()==1){
        return res;
    }
    vector<int> index;
    for(int i=0; i<num.size(); ++i)
        index.push_back(i);

    bool end = false;
    while(!end){
        // find the one with cloest greater than it
        int j;
        for(j=index.size()-1; j>=0; --j){
            int curNum = index[j];
            int cloestIndex = -1;
            for(int t=j+1; t<index.size(); ++t){
                if(index[t] > curNum){
                    if(cloestIndex == -1){
                        cloestIndex = t;
                    }
                    else if((index[cloestIndex]-curNum) > 
                        (index[t]-curNum)){
                            cloestIndex = t;
                    }
                }
            }
            if(cloestIndex != -1){
                // swap
                char tmp = index[j];
                index[j] = index[cloestIndex];
                index[cloestIndex] = tmp;
                // sort
                sort(index.begin() + j + 1, index.end());
                vector<int> ans;
                for(int ii=0; ii<index.size(); ++ii)
                    ans.push_back(num[index[ii]]);
                res.push_back(ans);
                break;
            }
        }
        if(j<0)
            break;
    }
    return res;
}


int main(){
    return 0;
}